package com.nycreativity.elecreativity.service.impl;

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.nycreativity.elecreativity.entity.po.ShoppingCart;
import com.nycreativity.elecreativity.mapper.ShoppingCartMapper;
import com.nycreativity.elecreativity.service.ShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nycreativity.elecreativity.entity.param.PageParam
import com.nycreativity.elecreativity.entity.param.shoppingcart.ShoppingCartAddParam
import com.nycreativity.elecreativity.entity.param.shoppingcart.ShoppingCartUpdateParam
import com.nycreativity.elecreativity.entity.vo.PageDataVO
import com.nycreativity.elecreativity.entity.vo.shoppingcart.ShoppingCartVO
import com.nycreativity.elecreativity.util.KtWrapperUtil
import com.nycreativity.elecreativity.util.PageUtil
import com.nycreativity.elecreativity.util.usersession.UserContext
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional
import javax.annotation.Resource

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-03-11
 */
@Service
class ShoppingCartServiceImpl : ServiceImpl<ShoppingCartMapper, ShoppingCart>(), ShoppingCartService {

    @Resource
    lateinit var shoppingCartMapper: ShoppingCartMapper

    @Transactional
    override fun addShoppingCart(shoppingCartAddParams: Array<ShoppingCartAddParam>): Boolean {
        val userId = UserContext.get().id ?: return false
        shoppingCartAddParams.forEach {
            var shoppingCart = getOne(
                KtQueryWrapper(ShoppingCart::class.java)
                    .eq(ShoppingCart::userId, userId)
                    .eq(ShoppingCart::skuId, it.skuId)
            )
            shoppingCart?.let { sc ->
                sc.count = sc.count?.plus(it.count) ?: it.count
                sc.modify()
                shoppingCartMapper.updateById(sc)
            } ?: run {
                shoppingCart = ShoppingCart(userId, it.goodsId, it.skuId, it.count)
                shoppingCart.create()
                shoppingCartMapper.insert(shoppingCart)
            }

        }
        return true
    }

    override fun queryShoppingCart(pageParam: PageParam): ShoppingCartVO {
        val user = UserContext.get() ?: return ShoppingCartVO(PageDataVO())
        val shoppingCartVOs = PageUtil.convert(
            shoppingCartMapper.selectShoppingCartInfo(
                Page(pageParam.currentPage.toLong(), pageParam.pageSize.toLong()),
                user.id
            )
        )
        return ShoppingCartVO(shoppingCartVOs)
    }

    @Transactional
    override fun removeShoppingCartItems(skuIds: Array<Long>): Boolean {
        val user = UserContext.get() ?: return false
        skuIds.forEach {
            val wrapper = KtWrapperUtil.ktDeleteWrapperForUpdate<ShoppingCart>()
                .eq(ShoppingCart::skuId, it)
                .eq(ShoppingCart::userId, user.id)
            update(null, wrapper)
        }
        return true
    }

    override fun updateShoppingCartItem(id: Long, shoppingCartUpdateParam: ShoppingCartUpdateParam): Boolean {
        val user = UserContext.get() ?: return false
        val shoppingCart = ShoppingCart(
            user.id,
            skuId = shoppingCartUpdateParam.skuId,
            count = shoppingCartUpdateParam.count
        )
        shoppingCart.id = id
        shoppingCart.modify()
        return shoppingCart.updateById()
    }

}
